package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomerBean;
import com.project.mapper.ICustomerMapper;
import com.project.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private ICustomerMapper customerMapper;
    @Override
    public IPage<CustomerBean> findByItem(Integer pageNO, String name, String cusRange) {
        QueryWrapper qw = new QueryWrapper();
        if (name!=null&&name.length()!=0){
            qw.like("c_name",name);
        }
        if (cusRange!=null&&cusRange.length()!=0){
            qw.like("c_range",cusRange);
        }
        return customerMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public CustomerBean findById(Integer id) {
        return customerMapper.selectById(id);
    }

    @Override
    public void update(Integer id, String leadingCadre, String phone, String cusRange) {
        CustomerBean customerBean = customerMapper.selectById(id);
        customerBean.setLeadingCadre(leadingCadre);
        customerBean.setPhone(phone);
        customerBean.setCusRange(cusRange);
        customerMapper.updateById(customerBean);
    }

    @Override
    public boolean add(CustomerBean customer) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("c_name",customer.getName());
        CustomerBean customerBean = customerMapper.selectOne(qw);
        if (customerBean==null){
            customerMapper.insert(customer);
            return true;
        }
        return false;
    }

    @Override
    public List<CustomerBean> findAll() {
        return customerMapper.selectList(null);
    }
}
